package com.artron.mediaartron.data.entity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-11-01.
 */

public class MaterialData implements IsLoadMore<MaterialWithDate> {


    /**
     * materialList : [{"originalImageWidth":720,"uploadDate":"2017-11-01","scale":0.972973,"originalImageHeight":917,"originalImagePath":"http://test2.artup.com/original/2017/11/1/45384e41fed4448bae34fafbb3053d21/8781da43daaa468b89bd4384737c5bf6.jpg","smallThumbnailImagePath":"http://test2.artup.com/thumbnail/2017/11/1/45384e41fed4448bae34fafbb3053d21/3bf8e0d1eed343d092e4b1722899c102_200.jpg","id":"b8a2e50aca7b403c9b388ee82e2dcfcc","bigThumbnailImagePath":"http://test2.artup.com/thumbnail/2017/11/1/45384e41fed4448bae34fafbb3053d21/3bf8e0d1eed343d092e4b1722899c102_740.jpg"},{"originalImageWidth":1074,"uploadDate":"2017-11-01","scale":1.45135,"originalImageHeight":1322,"originalImagePath":"http://test2.artup.com/original/2017/11/1/45384e41fed4448bae34fafbb3053d21/990573ec855142ca9837e8247794d053.jpg","smallThumbnailImagePath":"http://test2.artup.com/thumbnail/2017/11/1/45384e41fed4448bae34fafbb3053d21/f378ae221c7f4276a217adabb4644de3_200.jpg","id":"857c83f9178f4ecab41523b6fe00b758","bigThumbnailImagePath":"http://test2.artup.com/thumbnail/2017/11/1/45384e41fed4448bae34fafbb3053d21/f378ae221c7f4276a217adabb4644de3_740.jpg"},{"originalImageWidth":1824,"uploadDate":"2017-11-01","scale":2.46486,"originalImageHeight":2889,"originalImagePath":"http://test2.artup.com/original/2017/11/1/45384e41fed4448bae34fafbb3053d21/cccc536e3f8045c6a76ee5ccf9d22ea6.jpg","smallThumbnailImagePath":"http://test2.artup.com/thumbnail/2017/11/1/45384e41fed4448bae34fafbb3053d21/124fc13caf86491c8747823153f0f3de_200.jpg","id":"b49576855f3143b8a33c88cc7f376def","bigThumbnailImagePath":"http://test2.artup.com/thumbnail/2017/11/1/45384e41fed4448bae34fafbb3053d21/124fc13caf86491c8747823153f0f3de_740.jpg"},{"originalImageWidth":4608,"uploadDate":"2017-11-01","scale":3.5027,"originalImageHeight":2592,"originalImagePath":"http://test2.artup.com/original/2017/11/1/45384e41fed4448bae34fafbb3053d21/84d2cb3dd0f34d60ba73c07741da1a8b.jpg","smallThumbnailImagePath":"http://test2.artup.com/thumbnail/2017/11/1/45384e41fed4448bae34fafbb3053d21/af24a66cefca4f23afaa52eacce1f72f_200.jpg","id":"31e5f90518d94e5c8ea82a63ddd52fe0","bigThumbnailImagePath":"http://test2.artup.com/thumbnail/2017/11/1/45384e41fed4448bae34fafbb3053d21/af24a66cefca4f23afaa52eacce1f72f_740.jpg"},{"originalImageWidth":720,"uploadDate":"2017-11-01","scale":0.972973,"originalImageHeight":917,"originalImagePath":"http://test2.artup.com/original/2017/11/1/45384e41fed4448bae34fafbb3053d21/2e78f5ec134448158963caeb0859a2de.jpg","smallThumbnailImagePath":"http://test2.artup.com/thumbnail/2017/11/1/45384e41fed4448bae34fafbb3053d21/21af2057e5534fffa65198570566600c_200.jpg","id":"ed4d33c8e1ec43baaad94009a77e9b67","bigThumbnailImagePath":"http://test2.artup.com/thumbnail/2017/11/1/45384e41fed4448bae34fafbb3053d21/21af2057e5534fffa65198570566600c_740.jpg"},{"originalImageWidth":1400,"uploadDate":"2017-11-01","scale":1.33784,"originalImageHeight":990,"originalImagePath":"http://test2.artup.com/original/2017/11/1/45384e41fed4448bae34fafbb3053d21/f3587696286040b998592534e5093a69.jpg","smallThumbnailImagePath":"http://test2.artup.com/thumbnail/2017/11/1/45384e41fed4448bae34fafbb3053d21/d8e9fa48112e42d6b4b975b2c04d41ba_200.jpg","id":"064593692b3c48df89745daa91b64169","bigThumbnailImagePath":"http://test2.artup.com/thumbnail/2017/11/1/45384e41fed4448bae34fafbb3053d21/d8e9fa48112e42d6b4b975b2c04d41ba_740.jpg"},{"originalImageWidth":4608,"uploadDate":"2017-11-01","scale":3.5027,"originalImageHeight":2592,"originalImagePath":"http://test2.artup.com/original/2017/11/1/45384e41fed4448bae34fafbb3053d21/dff0ae9ea7984a65bcf56ece11f5849d.jpg","smallThumbnailImagePath":"http://test2.artup.com/thumbnail/2017/11/1/45384e41fed4448bae34fafbb3053d21/74649df0406841dfafe0bc28aff2ff99_200.jpg","id":"e1059ba405a144dc981d736b5f11f7d4","bigThumbnailImagePath":"http://test2.artup.com/thumbnail/2017/11/1/45384e41fed4448bae34fafbb3053d21/74649df0406841dfafe0bc28aff2ff99_740.jpg"},{"originalImageWidth":4608,"uploadDate":"2017-11-01","scale":3.5027,"originalImageHeight":2592,"originalImagePath":"http://test2.artup.com/original/2017/11/1/45384e41fed4448bae34fafbb3053d21/ac321689c21b4047ada5ec64f2146b5b.jpg","smallThumbnailImagePath":"http://test2.artup.com/thumbnail/2017/11/1/45384e41fed4448bae34fafbb3053d21/5dce086faeb948ca9a1ccac06a551e06_200.jpg","id":"24a5591f83894e0bb42a8ed30040ca06","bigThumbnailImagePath":"http://test2.artup.com/thumbnail/2017/11/1/45384e41fed4448bae34fafbb3053d21/5dce086faeb948ca9a1ccac06a551e06_740.jpg"},{"originalImageWidth":1824,"uploadDate":"2017-11-01","scale":2.46486,"originalImageHeight":2889,"originalImagePath":"http://test2.artup.com/original/2017/11/1/45384e41fed4448bae34fafbb3053d21/6154de7fef6a425c8183e8998734bf13.jpg","smallThumbnailImagePath":"http://test2.artup.com/thumbnail/2017/11/1/45384e41fed4448bae34fafbb3053d21/3529581306784746b0be067544d82549_200.jpg","id":"334f8086746e43c98d289a9673aec937","bigThumbnailImagePath":"http://test2.artup.com/thumbnail/2017/11/1/45384e41fed4448bae34fafbb3053d21/3529581306784746b0be067544d82549_740.jpg"}]
     * isMore : false
     * usedSpaceSize : 21439777
     */

    private boolean isMore;
    private String usedSpaceSize;
    /**
     * originalImageWidth : 720
     * uploadDate : 2017-11-01
     * scale : 0.972973
     * originalImageHeight : 917
     * originalImagePath : http://test2.artup.com/original/2017/11/1/45384e41fed4448bae34fafbb3053d21/8781da43daaa468b89bd4384737c5bf6.jpg
     * smallThumbnailImagePath : http://test2.artup.com/thumbnail/2017/11/1/45384e41fed4448bae34fafbb3053d21/3bf8e0d1eed343d092e4b1722899c102_200.jpg
     * id : b8a2e50aca7b403c9b388ee82e2dcfcc
     * bigThumbnailImagePath : http://test2.artup.com/thumbnail/2017/11/1/45384e41fed4448bae34fafbb3053d21/3bf8e0d1eed343d092e4b1722899c102_740.jpg
     */

    private List<MaterialListBean> materialList;

    private List<MaterialWithDate> mFragmentData;

    public boolean isIsMore() {
        return isMore;
    }

    public void setIsMore(boolean isMore) {
        this.isMore = isMore;
    }

    public String getUsedSpaceSize() {
        return usedSpaceSize;
    }

    public void setUsedSpaceSize(String usedSpaceSize) {
        this.usedSpaceSize = usedSpaceSize;
    }

    public List<MaterialListBean> getMaterialList() {
        return materialList;
    }

    public void setMaterialList(List<MaterialListBean> materialList) {
        this.materialList = materialList;
    }

    @Override
    public boolean isMore() {
        return isMore;
    }

    @Override
    public List<MaterialWithDate> getList() {
        return filterDate(materialList);
    }

    public void setFragmentData(List<MaterialWithDate> fragmentData) {
        mFragmentData = fragmentData;
    }

    private List<MaterialWithDate> filterDate(List<MaterialListBean> sourceData) {
        List<MaterialWithDate> list = mFragmentData;
        List<MaterialListBean> contentData;
        MaterialWithDate material;

        Date previousParse = null, currentParse;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        if (list == null) {
            list = new ArrayList<>();
            material = new MaterialWithDate();
            contentData = new ArrayList<>();
            material.setContentData(contentData);
            list.add(material);
        } else {
            material = list.get(list.size() - 1);
            contentData = material.getContentData();
            try {
                previousParse = format.parse(material.getUploadDate());
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        for (MaterialListBean data : sourceData) {
            try {
                currentParse = format.parse(data.getUploadDate());
            } catch (ParseException e) {
                currentParse = previousParse;
            }
            if (previousParse == null) {
                previousParse = currentParse;
            }
            if (previousParse != null && previousParse.after(currentParse)) {
                contentData = new ArrayList<>();
                material = new MaterialWithDate();
                material.setContentData(contentData);
                list.add(material);
            }
            contentData.add(data);
            previousParse = currentParse;
            material.setUploadDate(data.getUploadDate());
        }
        return list;
    }

    public static class MaterialListBean {
        private boolean isChecked;
        private int originalImageWidth;
        private String uploadDate;
        private double scale;
        private int originalImageHeight;
        private String originalImagePath;
        private String smallThumbnailImagePath;
        private String id;
        private String bigThumbnailImagePath;

        public boolean isChecked() {
            return isChecked;
        }

        public void setChecked(boolean checked) {
            isChecked = checked;
        }

        public int getOriginalImageWidth() {
            return originalImageWidth;
        }

        public void setOriginalImageWidth(int originalImageWidth) {
            this.originalImageWidth = originalImageWidth;
        }

        public String getUploadDate() {
            return uploadDate;
        }

        public void setUploadDate(String uploadDate) {
            this.uploadDate = uploadDate;
        }

        public double getScale() {
            return scale;
        }

        public void setScale(double scale) {
            this.scale = scale;
        }

        public int getOriginalImageHeight() {
            return originalImageHeight;
        }

        public void setOriginalImageHeight(int originalImageHeight) {
            this.originalImageHeight = originalImageHeight;
        }

        public String getOriginalImagePath() {
            return originalImagePath;
        }

        public void setOriginalImagePath(String originalImagePath) {
            this.originalImagePath = originalImagePath;
        }

        public String getSmallThumbnailImagePath() {
            return smallThumbnailImagePath;
        }

        public void setSmallThumbnailImagePath(String smallThumbnailImagePath) {
            this.smallThumbnailImagePath = smallThumbnailImagePath;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getBigThumbnailImagePath() {
            return bigThumbnailImagePath;
        }

        public void setBigThumbnailImagePath(String bigThumbnailImagePath) {
            this.bigThumbnailImagePath = bigThumbnailImagePath;
        }

        @Override
        public String toString() {
            return "MaterialListBean{" +
                    "isChecked=" + isChecked +
                    ", originalImageWidth=" + originalImageWidth +
                    ", uploadDate='" + uploadDate + '\'' +
                    ", scale=" + scale +
                    ", originalImageHeight=" + originalImageHeight +
                    '}';
        }
    }

    @Override
    public String toString() {
        return "MaterialData{" +
                "isMore=" + isMore +
                ", usedSpaceSize='" + usedSpaceSize + '\'' +
                ", materialList=" + materialList +
                ", mFragmentData=" + mFragmentData +
                '}';
    }
}
